package com.xzx.study;

import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.xzx.study.dao.BlackListDao;
import com.xzx.study.db.BlackListDBHelper;
import com.xzx.study.po.BlackListInfo;

import java.util.List;

public class BlackListActivity extends ListActivity implements View.OnClickListener {

    private static final String TAG = BlackListActivity.class.getSimpleName();

    private ListView black_list_view;
    private Button black_list_add;
    private BlackListDao blackListDao;
    private List<BlackListInfo> data;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_black_list);

//        black_list_view = findViewById(R.id.black_list_view);
        black_list_view = getListView();
        black_list_add = findViewById(R.id.black_list_add);
        black_list_add.setOnClickListener(this);

        blackListDao = new BlackListDao(new BlackListDBHelper(this));
        data = blackListDao.getAll();

        black_list_view.setAdapter(baseAdapter);
        black_list_view.setOnCreateContextMenuListener(this);

    }

    // 创建上下文菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, 1, 0, "更新");
        menu.add(0, 2, 0, "删除");

        // TODO 得到长按的position
        AdapterView.AdapterContextMenuInfo contextMenuInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
        position = contextMenuInfo.position;
    }

    // 选择上下文菜单
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (item.getItemId() == 1) {
            handleUpdate();
        } else if (item.getItemId() == 2) {
            handleDelete();
        }
        return super.onContextItemSelected(item);
    }

    private void handleDelete() {
        Log.i(TAG, "=========删除");
        BlackListInfo blackListInfo = data.get(position);
        blackListDao.delete(blackListInfo.getId());
        data.remove(position);
        baseAdapter.notifyDataSetChanged();
    }

    private void handleUpdate() {
        Log.i(TAG, "=========更新");
        final BlackListInfo blackListInfo = data.get(position);
        final EditText editText = new EditText(this);
        editText.setHint(blackListInfo.getNumber());
        AlertDialog dialog = new AlertDialog.Builder(this).setTitle("更新黑名单")
                .setView(editText)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        blackListInfo.setNumber(editText.getText().toString());
                        blackListDao.update(blackListInfo);
                        baseAdapter.notifyDataSetChanged();
                    }
                })
                .setNegativeButton("取消", null).show();
    }

    @Override
    public void onClick(View v) {
        Log.i(TAG, "=========添加");
        final EditText editText = new EditText(this);
        editText.setHint("请输入");
        AlertDialog dialog = new AlertDialog.Builder(this).setTitle("添加黑名单")
                .setView(editText)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        BlackListInfo blackListInfo = new BlackListInfo(-1, editText.getText().toString());
                        blackListDao.add(blackListInfo);
                        // 一定要放到data中
                        data.add(0, blackListInfo);
                        baseAdapter.notifyDataSetChanged();
                    }
                })
                .setNegativeButton("取消", null).show();
    }

    private BaseAdapter baseAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(BlackListActivity.this, android.R.layout.simple_list_item_1, null);
            }
            TextView textView = convertView.findViewById(android.R.id.text1);
            textView.setText(data.get(position).getNumber());
            return convertView;
        }
    };
}
